﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERF;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FirstPerson.Objects {
    /// <summary>
    /// Represents a decal. [Work in progress]
    /// </summary>
    class Decal : Plane {
        private Vector3 basePosition;
        private Vector3 surfaceNormal;

        /// <summary>
        /// Creates a new instance of Decal, with the specified position, normal and texture.
        /// </summary>
        /// <param name="game">The game that this instance belongs to.</param>
        /// <param name="position">The position of the decal.</param>
        /// <param name="normal">The normal of the underlying surface on which the decal should be positioned.</param>
        /// <param name="texture">The asset name for the texture the decal should use.</param>
        public Decal(ERFGame game, Vector3 position, Vector3 normal, string texture)
            : base(game, normal) {
            this.basePosition = position;
            this.surfaceNormal = normal;

            this.scale = new Vector3(0.04f, 0.04f, 0.04f);
            this.position = position  + (normal * 0.0125f);
            this.SetEffect(texture);
            base.IsTransparent = true;
        }

        public override void DrawTransparent(GameTime gameTime)
        {
            base.CurrentGame.GraphicsDevice.RenderState.CullMode = CullMode.None;
            base.DrawTransparent(gameTime);
            base.CurrentGame.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
        }

        /// <summary>
        /// Sets the Effect for the decal.
        /// </summary>
        /// <param name="texture">The texture asset name.</param>
        private void SetEffect(string texture) {
            BasicShaderEffect effect = new BasicShaderEffect(base.CurrentGame);

            effect.AmbientColor = Color.White.ToVector3();
            effect.AmbientIntensity = 0.6f;
            effect.EnableLight = true;
            effect.EnableTexture = true;
            effect.Texture = base.CurrentGame.Content.Load<Texture2D>(texture);
            effect.LightDirection = new Vector3(0, 1, -1);

            base.effect = effect;
        }
    }
}
